package sync2ck

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"go-zero-demo/app/datacenter/cron/internal/svc"
	"go-zero-demo/app/datacenter/cron/internal/util/timeutil"
	"gorm.io/gorm"
)

type OceanMaterialCost struct {
	SvcContext *svc.ServiceContext
	SyncClient *ClientV2
	MySQL      *gorm.DB
}

func NewOceanMaterialCost(svcContext *svc.ServiceContext) *OceanMaterialCost {
	return &OceanMaterialCost{
		SvcContext: svcContext,
		MySQL:      svcContext.MySQL,
		SyncClient: &ClientV2{svcContext},
	}
}

func (s *OceanMaterialCost) Run(ctx context.Context) {
	removeCkTable := true

	if err := s.syncTableBaseV2(removeCkTable); err != nil {
		logx.Errorf("转换CK失败 err := ", err)
	}
}

func (s *OceanMaterialCost) syncTableBaseV2(removeOld bool) error {
	tableName := "mb_ocean_material_cost"
	partition := "PARTITION BY toYYYYMM(day_date)"
	orderField := "(material_id,day_date,advertiser_id)"

	err := s.SyncClient.Sync2CK([]string{tableName}, partition, orderField)

	return err
}


func (s *OceanMaterialCost) syncTableBase(removeOld bool) error {
	tableName := "mb_ocean_material_cost"
	tableNameCK := timeutil.GetTableNameForRand(tableName)

	tableSql := s.SyncClient.GetCKCreateTableSql(tableNameCK,
		" ",
		"PARTITION BY toYYYYMM(day_date)",
		"(material_id,day_date,advertiser_id)",
		"mb_ocean_material_cost")

	return s.SyncClient.Sync2CKBase(tableName, tableNameCK, tableSql, removeOld)
}
